#include <iostream>
#include <unistd.h>
#include<ctime>


int gval=100;

void fun()
{
    
}
// 新线程 
//0x1111-ox2222
void *threadStart(void *args)
{
    while (true)
    {
        
        //int x=rand()%5;

        //std::cout << "new tread running..."<<",pid "<<getpid()<<":"<<x << std::endl;
        std::cout << "new tread running..."<<",pid "<<getpid()
        <<",gval:"<<gval<<" , &gval: "<<&gval << std::endl;
       
        sleep(1);

        fun();

        // if(x==0)
        // {
        //     int *p=nullptr;
        //     *p=100;//野指针
        // }
    }
}

//0x3333-ox4444
int main()
{
    srand(time(nullptr));
    pthread_t tid1;
    pthread_create(&tid1, nullptr, threadStart, (void *)"thread-new");

    pthread_t tid2;
    pthread_create(&tid2, nullptr, threadStart, (void *)"thread-new");

    pthread_t tid3;
    pthread_create(&tid3, nullptr, threadStart, (void *)"thread-new");

    // 主线程
    while (true)
    {
        sleep(1);
        std::cout << "main tread running..."<<",pid "<<getpid() 
        <<",gval:"<<gval<<" , &gval: "<<&gval << std::endl;


      gval++;//修改
      sleep(1);
    }
    return 0;
}